﻿$(function () {
    $("#txtLoginName").blur(function () {
        if (this.value.trim().length > 0) {
            if (!VerificationLoginName()) {
                $("#loginNameError").html("* 请输入有效的邮箱或手机号码").css("color", "red").slideDown(1000);
            } else {
                $.ajax({
                    type: 'POST',
                    url: "/Action/Register.ashx",
                    data: { type: "1", loginName: this.value.trim() },
                    async: true,
                    success: function (responseData) {
                        if (responseData == 1) {
                            $("#loginNameError").html("* 用户名可用").css("color", "green").slideDown(1000);
                        }
                        else {
                            $("#loginNameError").html("* 用户名不可用，请重新输入").css("color", "red").slideDown(1000);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {//请求失败处理函数
                        alert("请求失败，无法获取分组数据");
                    }
                })
            }
            return;
        }
        $("#loginNameError").slideUp(1000);
    });
    $("#txtLoginPwd").blur(function () {
        if (this.value.length != 0) {
            var pwdStrength = VerificationLoginPwd();
            switch (pwdStrength) {
                case 0, 1:
                    $("#loginPwdMessage").html("* 密码强度：弱").css("color", "red").slideDown(1000);
                    break;
                case 2:
                    $("#loginPwdMessage").html("* 密码强度：中").css("color", "#FFBB00").slideDown(1000);
                    break;
                case 3:
                    $("#loginPwdMessage").html("* 密码强度：强").css("color", "green").slideDown(1000);
                    break;
                default:
                    $("#loginPwdMessage").html("* 请输入6-20个字符").css("color", "red").slideDown(1000);
                    break;
            }
            return;
        }
        $("#loginPwdMessage").slideUp(1000);
    });
    $("#txtLoginConfirmPwd").blur(function () {
        if (this.value.length > 0) {
            if (!VerificationLoginConfirmPwd()) {
                $("#loginConfirmPwdError").slideDown(1000);
                return;
            }
        }
        $("#loginConfirmPwdError").slideUp(1000);
    });
});

var VerificationLoginName = function () {
    var value = $("#txtLoginName").val().trim();
    var emailReg = /([A-Za-z0-9]+)@([A-Za-z0-9]+(\.[A-Za-z0-9]+)+)/;
    var phoneReg = /^[\d]{11}$/;
    if (!emailReg.test(value)) {
        if (!phoneReg.test(value)) {
            return false;
        }
    }
    return true;
};

var VerificationLoginPwd = function () {
    var value = $("#txtLoginPwd").val();
    if (value.length >= 6 && value.length <= 20) {
        var pwdStrength = 0;
        if (/[a-z]/.test(value)) {
            pwdStrength += 1;
        }
        if (/[A-Z]/.test(value)) {
            pwdStrength += 1;
        }
        if (/[\d]/.test(value)) {
            pwdStrength += 1;
        }
        if (/[\W]/.test(value)) {
            pwdStrength += 1;
        }
        return pwdStrength;
    } else {
        return -1;
    }
};

var VerificationLoginConfirmPwd = function () {
    if ($("#txtLoginConfirmPwd").val() != $("#txtLoginPwd").val()) {
        return false;
    }
    return true;
};

var Register = function () {
    if ($("#loginNameError").html() == "* 用户名不可用，请重新输入") {
        $("#loginNameError").hide();
        $("#loginNameError").slideDown(1000);
    }
    else if (!VerificationLoginName()) {
        $("#loginNameError").hide();
        $("#loginNameError").html("* 请输入有效的邮箱或手机号码").css("color", "red").slideDown(1000);
    }
    else if (VerificationLoginPwd() < 0) {
        $("#loginPwdMessage").hide();
        $("#loginPwdMessage").html("* 请输入6-20个字符").css("color", "red").slideDown(1000);
    }
    else if (!VerificationLoginConfirmPwd()) {
        $("#loginConfirmPwdError").hide();
        $("#loginConfirmPwdError").slideDown(1000);
    } else {
        var loginPwd = hex_md5($("#txtLoginPwd").val());
        $.ajax({
            type: 'POST',
            url: "/Action/Register.ashx",
            data: { type: "2", loginName: $("#txtLoginName").val().trim(), loginPwd: loginPwd },
            async: true,
            success: function (responseData) {
                if (responseData == 1 || responseData == -1) {
                    window.location = "index.aspx";
                }
                else if (responseData == 0) {
                    alert("注册失败，请重试");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {//请求失败处理函数
                alert("请求失败，无法获取分组数据");
            }
        })
    }
};